﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using GameMath;

namespace Models
{
    public class World
    {
        public delegate void Spawning(GameObject entity);
        public event Spawning SpawnEvent;

        public World(int tilesX, int tilesY)
        {
            Entities = new List<GameObject>();
            ToSpawn = new List<GameObject>();
            Grid = new Tile[tilesX, tilesY];
            TileX = tilesX;
            TileY = tilesY;

            for (int y = 0; y < tilesY; y++)
            {
                for (int x = 0; x < tilesX; x++)
                {
                    Tile tile = new Tile(new GamePointF(x * 64, y * 64), new Size(64, 64));
                    Grid[x, y] = tile;
                }
            }

            Building testBuilding = new Building(new GamePointF(100, 10), new Size(64, 64), this);
            Entities.Add(testBuilding);
        }

        public Tile[,] Grid { get; private set; }
        public List<GameObject> Entities { get; private set; }
        private List<GameObject> ToSpawn { get; set; }
        public int TileX { get; private set; }
        public int TileY { get; private set; }

        public void Update(GameTimer timer)
        {
            if (ToSpawn.Count > 0)
            {
                string debug = "debug";
            }


            Entities.AddRange(ToSpawn);
            ToSpawn.Clear();
            foreach (GameObject gobj in Entities)
            {
                gobj.Update(timer);
            }
        }

        public void SpawnEntity(GameObject entity)
        {
            ToSpawn.Add(entity);
            
            if (SpawnEvent != null)
            {
                SpawnEvent(entity);
            }
        }
    }
}
